$(function () {
    $("#jqGrid").Grid({
        url: '../banner/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '编号', name: 'id', index: 'id', width: 30},
            {
                label: 'Banner类型', name: 'type', index: 'type', width: 80,
                formatter: function (value) {
                    if (value === 1) {
                        return "商城";
                    } else if (value === 2) {
                        return "APP首页";
                    }
                    else if (value === 3) {
                        return "分类Banner";
                    }
                    else if (value === 4) {
                        return "SPCbanner";
                    }
                    else if (value === 6) {
                        return "排行";
                    }
                    else if (value === 7) {
                        return "行业案例";
                    } else if (value === 8) {
                        return "活动广告";
                    }
                }
            },
            {label: '描述', name: 'msg', index: 'msg', width: 80},
            {
                label: 'Banner图', name: 'fileUrl', index: 'fileUrl', width: 80,
                formatter: function (value) {
                    return transImg(value);
                }
            },
            {
                label: '是否显示', name: 'viewFlag', index: 'viewFlag', width: 40,
                formatter: function (value) {
                    if (value === 1)
                        return "启用";
                    else if (value === 0)
                        return "禁用";
                }
            },
            {label: '业务', name: 'business', index: 'business', width: 40},
            {
                label: '业务类型', name: 'businessType', index: 'businessType', width: 40,
                formatter: function (value) {
                    if (value === 0) {
                        return "点击不跳转";
                    } else if (value === 1) {
                        return "广告 图片+H5";
                    } else if (value === 2) {
                        return "视频";
                    }
                    else if (value === 3) {
                        return "商品";
                    }
                    else if (value === 4) {
                        return "帖子(动态)";
                    } else if (value === 5) {
                        return "活动";
                    }
                }
            },
            {label: '关键词', name: 'word', index: 'word', width: 40},
            {label: '页面地址', name: 'otherUrl', index: 'otherUrl', width: 40},
            {label: '标题', name: 'title', index: 'title', width: 40},
            {label: '短标题', name: 'shortTitle', index: 'shortTitle', width: 40},
            {
                label: '创建日期', name: 'createTime', index: 'createTime', width: 80, formatter: function (value) {
                    return transDate(value);
                }
            }
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        banner: {},
        ruleValidate: {
            msg: [
                {required: true, message: '描述不能为空', trigger: 'blur'}
            ],
            // type: [
            //     {required: true, message: '请选择类型', trigger: 'change'}
            // ],
            // viewFlag: [
            //     {required: true, message: '请选择是否显示', trigger: 'change'}
            // ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.banner = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.banner.id == null ? "../banner/save" : "../banner/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.banner),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../banner/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../banner/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.banner = r.banner;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.banner.fileUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.banner.fileUrl;
            eyeImage(url);
        },

        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
    }
});